<?php

namespace App\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Site;
use App\Models\Role;

/**
 * 角色
 * @package App\Api
 */
class RoleController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:sanctum');
        $this->authorizeResource(Role::class, 'role');
    }

    public function index(Site $site)
    {
        return Role::where('site_id', $site['id'])->with('permissions')->get();
    }

    public function store(Request $request, Site $site)
    {
        $role = Role::create(['name' => $request->name, 'title' => $request->title, 'site_id' => $site['id']]);
        return $this->message('角色创建成功', 0, $role);
    }

    public function show(Site $site, Role $role)
    {
        return $role->load('permissions');
    }

    public function update(Request $request, Site $site, Role $role)
    {
        $role['name'] = $request->name;
        $role['title'] = $request->title;
        $role->save();
        return $this->message('角色更新成功');
    }

    public function destroy(Site $site, Role $role)
    {
        $role->delete();
        return $this->message('角色删除成功');
    }
}
